﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class AddDepositInterestModel
    {
        public AddDepositInterestModel()
        {
            init();
        }

        public void init()
        {
            if (Header == null)
                Header = new DepositHeader();
            if (InterestAdaters == null)
                InterestAdaters = new List<DepositInterestAdapter>();
            if (InterestPaymentDateAdapters == null)
                InterestPaymentDateAdapters = new List<DepositInterestAdapter>();
            if (DepositInterest == null)
                DepositInterest = new DepositInterest();
        }

        public static AddDepositInterestModel BuildModel(DepositInterestScreenData data)
        {
            var model = new AddDepositInterestModel();

            model.Header = data.Header;
            model.Interests = data.ListInterest;

            return model;
        }

        public DepositHeader Header { get; set; }

        public IList<DepositInterest> Interests
        {
            set
            {
                IList<DepositInterest> data = value;
                if (data != null && data.Count > 0)
                {
                    InterestAdaters.Clear();
                    InterestPaymentDateAdapters.Clear();
                    foreach (var item in data)
                    {
                        if (item.InterestApplyDate.HasValue)
                            InterestAdaters.Add(new DepositInterestAdapter(item));
                        if (item.InterestPaymentDate.HasValue)
                            InterestPaymentDateAdapters.Add(new DepositInterestAdapter(item));
                    }
                    InterestPaymentDateAdapters = InterestPaymentDateAdapters.OrderByDescending(x => x.PaymentDate).ToList();
                }
            }
        }

        public IList<DepositInterestAdapter> InterestPaymentDateAdapters { get; set; }

        public IList<DepositInterestAdapter> InterestAdaters { get; set; }

        public DepositInterest DepositInterest { get; set; }

        #region Text Field

        public int? HeaderID
        {
            get { return Header.DepositHeaderID; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public DateTime? ApplyDate
        {
            get { return DepositInterest.InterestApplyDate; }
            set { DepositInterest.InterestApplyDate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? InterestRate
        {
            get { return DepositInterest.InterestRate; }
            set { DepositInterest.InterestRate = value; }
        }

        #endregion

        public bool UpdateMode { get; set; }

        public int? DeleteID { get; set; }
        public string BtnSubmit { get; set; }
    }

}